package imdbfolder.gui;

import imdbfolder.data.Movie;
import imdbfolder.data.MovieSelectionMap;
import imdbfolder.gui.icons.IconStore;
import imdbfolder.gui.util.BoldLabel;
import imdbfolder.gui.util.Hyperlink;
import imdbfolder.gui.util.IconLink;

import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.UIDefaults;
import javax.swing.UIManager;

import net.miginfocom.swing.MigLayout;

public class MoviePanel extends JPanel {

    private final MovieController controller;

    private static final long serialVersionUID = 1L;

    private IconLink openFileIcon;

    private IconLink refreshIcon;

    private IconLink cancelIcon;

    public MoviePanel(MovieController movieController) {
        this.controller = movieController;

        setLayout(new MigLayout("ins 0, aligny top, fillx"));
        
        initialize();
        
    }
    
    public void initialize() {
        removeAll();
      
        Movie movie = this.controller.getMovie();
       
        UIDefaults uiDefaults = UIManager.getDefaults(); 
       
        uiDefaults.put( "Label.font", 
                       ((Font)uiDefaults.get("Label.font")).deriveFont(12.5f) );
        
        Hyperlink titleLink = new Hyperlink(movie.getTitle() + " (" + movie.getYear() + ")", movie.getImdbUrl());
        titleLink.setFont(titleLink.getFont().deriveFont(16f));

        if (movie.isFetchedInfo()) {
            String imagePath = MovieSelectionMap.getInstance().getImagePath(movie);
            add(new JLabel(new ImageIcon(imagePath)), "left, spany, top, w 240px!");

            add(titleLink, "spanx, wrap");

            add(new BoldLabel("Score:"), "top");
    
            JProgressBar progressBar = new JProgressBar(0, 100);
            progressBar.setValue(Math.round(movie.getScore() * 10));
            progressBar.setString("" + movie.getScore());
            progressBar.setStringPainted(true);

            add(progressBar, "split, top");
            add(new JLabel(" with " + movie.getNumVotes() + " votes"), "top, wrap");

            add(new BoldLabel("Runtime:"), "top");
            add(new JLabel(movie.getRuntime() + " min"), "top, split 3, gapafter 20");

            add(new BoldLabel("Genre:"), "top");
            add(new JLabel(movie.getGenres()), "top, wrap");
            
            add(new BoldLabel("Director:"), "top");
            add(new JLabel("<html>" + movie.getDirectors() + "</html>"), "top, wrap");
            
            add(new BoldLabel("Actors:"), "top");
            add(new JLabel("<html>" + movie.getActors() + "</html>"), "top, wrap, wmax 800");

            add(new BoldLabel("Plot:"), "top");
            add(new JLabel("<html>" + movie.getPlotSummary() + "</html>"), "top, wrap, wmax 800, gapbottom 10lp");

            openFileIcon = new IconLink(IconStore.get("drive_go.png"), "Open");
            add(openFileIcon, "skip 2, split 3, top, gapafter 10lp");

            refreshIcon = new IconLink(IconStore.get("arrow_refresh.png"), "Refresh");
            add(refreshIcon, "top, gapafter 10lp");

            cancelIcon = new IconLink(IconStore.get("magnifier.png"), "Search again");
            add(cancelIcon, "top, wrap");

            observeButtons();
        }
        else {
            add(titleLink, "wrap");
        }

        revalidate();
    }
    
    

    private void observeButtons() {
        openFileIcon.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                controller.openMovie();
            }
        });

        refreshIcon.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                controller.updateMovieInfo();
            }
        });

        cancelIcon.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                controller.clearMovieInfo();
            }
        });
    }

    public void setUpdating() {
        add(new JLabel("Updating..."), "split, wrap");
        revalidate();
    }
}
